package com.yellowpage.alarm.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.alarm.data.AlarmInfo;
import com.yellowpage.alarm.service.AlarmInfoService;
import com.yellowpage.ca.data.Dictionary;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.DictionaryService;
import com.yellowpage.ca.service.UserService;

public class AlarmInfoAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<AlarmInfo> alarmInfoList = new ArrayList<AlarmInfo>();
	private List<Map> resultList = new ArrayList<Map>();

	private AlarmInfo alarmInfo;
	private List<Dictionary> alarmTypeList;
	private List<Dictionary> alarmLevelList;
	private List<Dictionary> alarmStatusList;
	private List<Dictionary> informTypeList;
	private User lastUpdateUser;

	private AlarmInfoService alarmInfoService;
	private DictionaryService dictionaryService;
	private UserService userService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_alarmType;
	private String query_alarmLevel;
	private String query_cause;
	private String query_status;

	public void prepare() {
		super.prepare();

		alarmTypeList = dictionaryService.getByDictType("ALARM_TYPE");
		alarmLevelList = dictionaryService.getByDictType("ALARM_LEVEL");
		alarmStatusList = dictionaryService.getByDictType("ALARM_STATUS");

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.alarmInfo = alarmInfoService.getAlarmInfo(Long
					.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("alarmType", query_alarmType);
		queryMap.put("alarmLevel", query_alarmLevel);
		queryMap.put("cause", query_cause);
		queryMap.put("status", query_status);
		queryMap.put("userId", userId);

		resultList = alarmInfoService
				.queryAlarmInfoByPage(queryMap, pageRender);
		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		alarmInfo = alarmInfoService.getAlarmInfo(id);
		informTypeList = dictionaryService.getByDictType("INFORM_TYPE");
		lastUpdateUser = alarmInfo.getLastUpdateUser() == null ? new User()
				: userService.getUser(alarmInfo.getLastUpdateUser());
		return RESULT_EDIT;
	}

	public String update() {

		try {
			alarmInfo.setLastUpdateUser(userId);
			Date now = Calendar.getInstance().getTime();
			if (Constants.get("ALARMINFO_STATUS_INIT").equals(alarmInfo.getStatus())) {
				alarmInfo.setConfirmTime(null);
				alarmInfo.setRecoverTime(null);
			}
			// 确认
			else if (Constants.get("ALARMINFO_STATUS_CONFIRM").equals(alarmInfo
					.getStatus())) {
				if (alarmInfo.getConfirmTime() == null)
					alarmInfo.setConfirmTime(now);
				alarmInfo.setRecoverTime(null);
			}
			// 失效
			else if (Constants.get("ALARMINFO_STATUS_DISABLED").equals(alarmInfo
					.getStatus())) {
				if (alarmInfo.getConfirmTime() == null)
					alarmInfo.setConfirmTime(now);
				if (alarmInfo.getRecoverTime() == null)
					alarmInfo.setRecoverTime(now);
			}

			alarmInfoService.updateAlarmInfo(alarmInfo);
			this.addActionMessage("告警信息修改成功!");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.addActionError("** 告警信息修改失败!");
		}

		return toEdit();
	}

	public void validateUpdate() {
		if (alarmInfo.getAlarmType().length() == 0) {
			this.addActionError("** 告警类别 不能为空 !");
		}
		if (alarmInfo.getAlarmLevel().length() == 0) {
			this.addActionError("** 级别 不能为空 !");
		}
		if (alarmInfo.getCause().length() == 0) {
			this.addActionError("** 告警原因描述 不能为空 !");
		}
		if (alarmInfo.getStatus().length() == 0) {
			this.addActionError("** 状态 不能为空 !");
		}
		if (alarmInfo.getInformType().length() == 0) {
			this.addActionError("** 通知机制 不能为空 !");
		}
		this.setValidatorResult("/alarm/alarminfo/AlarmInfoUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.alarmInfoService.delete(ids);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.addActionError("** 删除失败,记录可能还有约束冲突 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setAlarmInfo(AlarmInfo alarmInfo) {
		this.alarmInfo = alarmInfo;
	}

	public AlarmInfo getAlarmInfo() {
		return alarmInfo;
	}

	public void setAlarmInfoService(AlarmInfoService alarmInfoService) {
		this.alarmInfoService = alarmInfoService;
	}

	public List<AlarmInfo> getAlarmInfoList() {
		return alarmInfoList;
	}

	public void setAlarmInfoList(List<AlarmInfo> alarmInfoList) {
		this.alarmInfoList = alarmInfoList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_alarmType() {
		return this.query_alarmType;
	}

	public void setQuery_alarmType(String alarmType) {
		this.query_alarmType = alarmType;
	}

	public String getQuery_alarmLevel() {
		return this.query_alarmLevel;
	}

	public void setQuery_alarmLevel(String alarmLevel) {
		this.query_alarmLevel = alarmLevel;
	}

	public String getQuery_cause() {
		return this.query_cause;
	}

	public void setQuery_cause(String cause) {
		this.query_cause = cause;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public List<Dictionary> getAlarmTypeList() {
		return alarmTypeList;
	}

	public List<Dictionary> getAlarmLevelList() {
		return alarmLevelList;
	}

	public List<Dictionary> getAlarmStatusList() {
		return alarmStatusList;
	}

	public List<Dictionary> getInformTypeList() {
		return informTypeList;
	}

	public User getLastUpdateUser() {
		return lastUpdateUser;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
